﻿using System;
using System.Threading;

namespace VS2048
{
    /// <summary>
    /// 上下左右移动的时候，不需要知道那个cell获取了焦点，
    /// 而是根据算法，比如，向左移动，那么最左边的两个相同，则合并
    /// 相当于MVC里面的C
    /// </summary>
    internal class PlayRuleController
    {
        private int _rowNum = 0;
        private int _colNum = 0;

        private PlayBox _playBox = null;
        private CellsHolder _cellsmodel = new CellsHolder();
        private CellStyleDictionary _cellStyles = new CellStyleDictionary();

        private GameSummaryInfo _sumInfo;

        private Random _rdm = null;
        private object _lockObj = new object();

        public Action<GameSummaryInfo> OnGameSummaryInfoChanged;

        public GameSummaryInfo CurGameSummaryInfo
        {
            get { return _sumInfo; }
        }

        public ICell[,] Cells
        {
            get { return _cellsmodel.Cells; }
        }

        public PlayRuleController(PlayBox playbox, int cellRowNum, int cellColNum)
        {
            _rowNum = cellRowNum;
            _colNum = cellColNum;

            _playBox = playbox;
            _cellsmodel.RowNum = cellRowNum;
            _cellsmodel.ColumnNum = cellColNum;
            _cellsmodel.CellMergeStrategy = new DirectionCellMergeStrategy();
        }

        public void ClearModel()
        {
            if (_cellsmodel != null)
                _cellsmodel.ClearCell();
            _sumInfo = new GameSummaryInfo();
        }

        public void DoKeyUp(int keyVal)
        {
            ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(DoNextStep), keyVal);
        }

        private void DoNextStep(object keyVal)
        {
            lock (_lockObj)
            {
                if (_rdm == null)
                    _rdm = new Random();
                int rdmFactor = _rdm.Next(10);
                int seed = 2;
                switch (rdmFactor)
                { 
                    case 1:
                        seed = 4;
                        break;
                    case 2:
                        seed = 8;
                        break;
                    case 3:
                        seed = 16;
                        break;
                    //case 4:
                    //    seed = 32;
                    //    break;
                    //case 5:
                    //    seed = 64;
                    //    break;
                }
                
                int rowfactor = _rdm.Next(_rowNum);
                int colfactor = _rdm.Next(_colNum);

                //首先合并；
                int currScore = _cellsmodel.MergeNearCell((int)keyVal);
                _playBox.RefreshMe();
                //
                while (!_cellsmodel.CheckPosition(rowfactor, colfactor))//问题：导致Cell满后无法合并
                {
                    rowfactor = _rdm.Next(_rowNum);
                    colfactor = _rdm.Next(_colNum);
                }
                ICell cell = new PaintedCell();
                cell.Coordinate = new CellCoordinate(rowfactor, colfactor);
                cell.DisplayNum = seed.ToString();
                cell.TextFont = _cellStyles[seed].TextFont;
                cell.BackColor = _cellStyles[seed].BackColor;
                cell.TextColor = _cellStyles[seed].TextColor;
                cell.Shape = CellShape.Ellipse;
                //接着添加新的
                //System.Threading.Thread.Sleep(1);
                _cellsmodel.AddCell(cell.Coordinate, cell);
                _playBox.UpdateCell(cell);

                _sumInfo.TotalCellCount += 1;
                _sumInfo.CurrentCellCount = _cellsmodel.GetCurrCellCount();
                _sumInfo.NewCellLocation = cell.Coordinate;
                _sumInfo.Score += currScore;

                if (OnGameSummaryInfoChanged != null)
                    OnGameSummaryInfoChanged(_sumInfo);
            }
        }
    }
}
